package top.lyc829.zzl_android.ui.thing.travels;

import androidx.lifecycle.ViewModelProvider;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import top.lyc829.zzl_android.R;
import top.lyc829.zzl_android.databinding.TravelsFragmentBinding;
import top.lyc829.zzl_android.logic.util.PageInfo;
import top.lyc829.zzl_android.logic.util.Utils;
import top.lyc829.zzl_android.ui.thing.ThingViewModel;
import top.lyc829.zzl_android.ui.thing.dynamic.DynamicRecyclerViewAdapter1;
import top.lyc829.zzl_android.ui.thing.travel_detail.TravelDetailActivity;

public class TravelsFragment extends Fragment {

    private TravelsViewModel mViewModel;
    private ThingViewModel thingViewModel;
    private TravelsFragmentBinding binding;
    private PageInfo pageInfo;
    TravelsAdapter adapter;


    public static TravelsFragment newInstance() {
        return new TravelsFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        binding = TravelsFragmentBinding.inflate(getLayoutInflater());
        return binding.getRoot();

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = new ViewModelProvider(this).get(TravelsViewModel.class);
        thingViewModel = new ViewModelProvider(requireActivity()).get(ThingViewModel.class);

        pageInfo = new PageInfo();
        initTravelRecyclerAdapter();

        observer();

        clickListener();


    }

    private void clickListener() {
        adapter.setOnItemClickListener((adapter1, view, position) -> {
            Intent intent = new Intent(getActivity(), TravelDetailActivity.class);
            intent.putExtra("travel_id", adapter.getData().get(position).getId());
            startActivity(intent);
        });
    }

    private void observer() {

        mViewModel.getTravelsLiveData().observe(getViewLifecycleOwner(), travelsSimpleBeans -> {
            if (pageInfo.isFirstPage()) {
                adapter.setList(travelsSimpleBeans);
                binding.refreshLayout.finishRefresh();
            } else {
                adapter.addData(travelsSimpleBeans);
                binding.refreshLayout.finishLoadMore();
            }
            pageInfo.nextPage();
        });

        thingViewModel.getTravelsDoubleClickLiveData().observe(getViewLifecycleOwner(), aBoolean -> {
            if (aBoolean) {
                binding.travelRecyclerview.scrollToPosition(0);
                binding.refreshLayout.autoRefresh();
            }
        });

    }

    private void initTravelRecyclerAdapter() {
        adapter = new TravelsAdapter();
        binding.travelRecyclerview.setLayoutManager(new LinearLayoutManager(getContext()));
        binding.travelRecyclerview.setAdapter(adapter);
        binding.refreshLayout.autoRefresh();

        binding.refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                request();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pageInfo.reset();
                request();
            }
        });
    }

    private void request() {
        try {
            mViewModel.loadTravelItems(pageInfo.getPage());
        } catch (Exception e) {
            Utils.Sout(e.getMessage());
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }
}